
* replicates Figure A1 in the online appendix
* note: requires running the entire code at once, given the use of local macros

clear *
set matsize 1000

/*-------------------------------------------------------------------------------------*/
/* 2016 Survey of Consumer Finances (summary file) */
/*-------------------------------------------------------------------------------------*/

use scf2016, clear

* recreate YouGov's version of assets question
gen double gross_nore = asset - (houses+oresre+nnresre)

collapse wgt ///
	income gross_nore ///
	retqliq irakh thrift futpen currpen ///
	debt mrthel resdbt othloc ccbal install odebt ///
	wageinc bussefarminc intdivinc kginc ssretinc transfothinc penacctwd, ///
	by(YY1)

* YouGov's version of binned income
gen incomeall = .
replace incomeall = 1 if income < 10000 & income ~= .
replace incomeall = 2 if income >= 10000 & income < 20000
replace incomeall = 3 if income >= 20000 & income < 30000
replace incomeall = 4 if income >= 30000 & income < 40000
replace incomeall = 5 if income >= 40000 & income < 50000
replace incomeall = 6 if income >= 50000 & income < 60000
replace incomeall = 7 if income >= 60000 & income < 70000
replace incomeall = 8 if income >= 70000 & income < 80000
replace incomeall = 9 if income >= 80000 & income < 100000
replace incomeall = 10 if income >= 100000 & income < 120000
replace incomeall = 11 if income >= 120000 & income < 150000
replace incomeall = 12 if income >= 150000 & income < 200000
replace incomeall = 13 if income >= 200000 & income < 250000
replace incomeall = 14 if income >= 250000 & income < 350000
replace incomeall = 15 if income >= 350000 & income < 500000
replace incomeall = 16 if income >= 500000 & income < 750000
replace incomeall = 17 if income >= 750000 & income < 1000000
replace incomeall = 18 if income >= 1000000 & income ~= .

* YouGov's version of binned assets
gen assetsall = .
replace assetsall = 1 if gross_nore < 250000 & gross_nore ~= .
replace assetsall = 2 if gross_nore >= 250000 & gross_nore < 500000
replace assetsall = 3 if gross_nore >= 500000 & gross_nore < 750000
replace assetsall = 4 if gross_nore >= 750000 & gross_nore < 1000000
replace assetsall = 5 if gross_nore >= 1000000 & gross_nore < 2000000
replace assetsall = 6 if gross_nore >= 2000000 & gross_nore < 5000000
replace assetsall = 7 if gross_nore >= 5000000 & gross_nore < 10000000
replace assetsall = 8 if gross_nore >= 10000000 & gross_nore < 25000000
replace assetsall = 9 if gross_nore >= 25000000 & gross_nore < 50000000
replace assetsall = 10 if gross_nore >= 50000000 & gross_nore ~= .

* YouGov's version of binned liabilities
gen liaball = .
replace liaball = 1 if debt < 250000 & debt ~= .
replace liaball = 2 if debt >= 250000 & debt < 500000
replace liaball = 3 if debt >= 500000 & debt < 750000
replace liaball = 4 if debt >= 750000 & debt < 1000000
replace liaball = 5 if debt >= 1000000 & debt < 2000000
replace liaball = 6 if debt >= 2000000 & debt < 5000000
replace liaball = 7 if debt >= 5000000 & debt < 10000000
replace liaball = 8 if debt >= 10000000 & debt < 25000000
replace liaball = 9 if debt >= 25000000 & debt < 50000000
replace liaball = 10 if debt >= 50000000 & debt ~= .

* YouGov's version of main source of wealth
gen wage = wageinc
gen business = bussefarminc
gen invest = intdivinc + kginc 
gen ssret = ssretinc + penacctwd + transfothinc
gen other = income-(wageinc + business + invest)
egen max = rowmax(wage business invest other)
foreach x in wage business invest other {
	gen `x'_m = (`x' == max)
	}

gen weight = wgt*1/.00000001

* program to summarize variables
capture program drop tabs
program define tabs
	args x y
	tab `x' [fw=weight], gen(`x'_)
	local max = r(r)
	mat mat = J(`max',1,.)
	forval i = 1/`max' {
		mean `x'_`i' [pw=wgt]
		mat tab = r(table)
		local b = tab[1,1]
		mat mat[`i',1] = `b'
		drop `x'_`i'
		}
	clear
	svmat mat
	egen `x' = seq(), from(1)
	rename mat1 `y'
	replace `y' = `y'*100
end
	
* put into separate sheets for graphing

* distribution of assets given income
preserve
keep if incomeall >= 15
tabs assetsall scf
tempfile scf_assets
save `scf_assets', replace
restore

* distribution of income given assets
preserve
keep if assetsall >= 6
tabs incomeall scf
tempfile scf_income
save `scf_income', replace
restore

* liabilities given income or assets
preserve
keep if incomeall >= 15 | assetsall >= 6
tabs liaball scf
tempfile scf_liab
save `scf_liab', replace
restore

* main source of income given income or assets
preserve
keep if incomeall >= 15 | assetsall >= 6
mean wage_m business_m invest_m other_m [pw=wgt]
mat mat = r(table)'
mat mat = mat[1..4,1..1]
clear
svmat mat
egen source = seq(), from(1)
rename mat1 scf
replace scf = scf*100
tempfile scf_source
save `scf_source', replace
restore

/*-------------------------------------------------------------------------------------*/
/* Our YouGov survey */
/*-------------------------------------------------------------------------------------*/

use survey, clear

drop weight
gen weight = 1
gen wgt = 1

* distribution of assets given income
preserve
keep if incomeall >= 15
tabs assetsall yougov
tempfile yg_assets
save `yg_assets', replace
restore

* distribution of income given assets
preserve
keep if assetsall >= 6
tabs incomeall yougov
tempfile yg_income
save `yg_income', replace
restore

* liabilities given income or assets
preserve
keep if wealthy == 1
tabs liaball yougov
tempfile yg_liab
save `yg_liab', replace
restore

* main source of income given income or assets
preserve
gen wage_m = (wealth_source == 2) if wealth_source ~= .
gen business_m = (wealth_source == 3) if wealth_source ~= .
gen invest_m = (wealth_source == 6 | wealth_source == 4) if wealth_source ~= .
gen ssret_m = (wealth_source == 7 | wealth_source == 8 | wealth_source == 9  | wealth_source == 1) ///
	if wealth_source ~= .
gen other_m = (wage_m == 0 & business_m == 0 & invest_m == 0) if wealth_source ~= .
keep if wealthy == 1
mean wage_m business_m invest_m other_m [pw=wgt]
mat mat = r(table)'
mat mat = mat[1..4,1..1]
clear
svmat mat
egen source = seq(), from(1)
rename mat1 yougov
replace yougov = yougov*100
tempfile yg_source
save `yg_source', replace
restore

/*-------------------------------------------------------------------------------------*/
/* Create graph */
/*-------------------------------------------------------------------------------------*/

* income given assets
use `scf_income', clear
merge 1:1 incomeall using `yg_income'
drop _m
gen income = 1 in 1/9
replace income = 2 in 10/12
replace income = 3 in 13/15
replace income = 4 in 16
replace income = 5 in 17/18
collapse (sum) scf yougov*, by(income)
la def income 1 "<$100K" 2 "$100-200K" 3 "$200-500K" 4 "$500-750K" ///
	5 ">$750K", replace
la val income income
graph bar (asis) scf yougov, over(income) label ///
	scheme(s1mono) name(g1, replace) title("(a) Household Income") ///
	legend(order(1 "SCF" 2 "Our survey") region(lcolor(none))) nodraw

* assets given income
use `scf_assets', clear
merge 1:1 assetsall using `yg_assets'
drop _m
gen assets = 1 in 1/4
replace assets = assetsall-3 in 5/7
replace assets = 5 in 8/10
collapse (sum) scf yougov*, by(assets)
la def assets 1 "<$1M" 2 "$1-2M" 3 "$2-5M" 4 "$5-10M" 5 ">$10M", replace
la val assets assets
graph bar (asis) scf yougov, over(assets) label ///
	scheme(s1mono) name(g2, replace) title("(b) Gross Liquid Assets") ///
	legend(off) nodraw

* liabilities
use `scf_liab', clear
merge 1:1 liaball using `yg_liab'
drop _m
gen liab = liaball
replace liab = 4 in 5/10
collapse (sum) scf yougov*, by(liab)
la def liab 1 "<$250K" 2 "$250-500K" 3 "$500-750K" 4 ">$750K", replace
la val liab liab
graph bar (asis) scf yougov, over(liab) label ///
	scheme(s1mono) name(g3, replace) title("(c) Liabilities") ///
	legend(off) nodraw

* main source of wealth
use `scf_source', clear
merge 1:1 source using `yg_source'
drop _m
la def source 1 "Salary" 2 "Business" 3 "Investments" 4 "Other", replace
la val source source
graph bar (asis) scf yougov, over(source) label ///
	scheme(s1mono) name(g4, replace) title("(d) Main Wealth Source") ///
	legend(off) nodraw

* combine all plots
grc1leg g1 g2, ycommon b1("") scheme(s1mono) name(p1, replace)
grc1leg g3 g4, ycommon b1("") scheme(s1mono) name(p2, replace)
grc1leg p1 p2, ycommon l1("Percent") b1("") rows(2) scheme(s1mono) scale(.98)
